import { Fight } from "../entity/fight";
import { GlobalConfig } from "../../config/global/global_config";
import { Monster } from "../entity/monster";
import { Player } from "../entity/player.entity";
import { QualityType } from "../consts/quality_type";
import { MonsterService } from "./monster_service";
import { Treasure } from "../entity/treasure.entity";
import net_result from "../consts/net_result";
import { Equipm } from "../entity/equipms/equipm.entity";
import { EquipmType } from "../consts/equipm_type";
import { Weapon } from "../entity/equipms/weapon";
import { Shield } from "../entity/equipms/shield";
import { EquipmService } from "./equipm_service";
import orm_util from "../utils/orm_util";
 

export class FightService{

    public static ins() : FightService{
        if (FightService._ins == null){
            FightService._ins = new FightService();
        }
        return FightService._ins;
    }
    private static _ins = null;

    
    _fightList={}


    public addFight(fight:Fight){
        this._fightList[fight.player.unitId]=fight;
    }

    public getFight(playerId):Fight{
        return this._fightList[playerId];
    }

    public removeFight(fight:Fight){
        delete this._fightList[fight.player.unitId]
    }
 
    public getNextEnemy(player:Player){
 
        // let monsterQuality=QualityType.白;
        // let isPlayer=false;
        // let ranNum= Math.random();
        // if(ranNum<GlobalConfig.ins().match[player.level].w){
        //     monsterQuality=QualityType.白;
        // }
        // else if(ranNum<(GlobalConfig.ins().match[player.level].w+GlobalConfig.ins().match[player.level].g)){
        //     monsterQuality=QualityType.绿;
        // }
        // else if(ranNum<(GlobalConfig.ins().match[player.level].w+GlobalConfig.ins().match[player.level].g+GlobalConfig.ins().match[player.level].p)){
        //     monsterQuality=QualityType.紫;
        // }
        // else if(ranNum<(GlobalConfig.ins().match[player.level].w+GlobalConfig.ins().match[player.level].g+GlobalConfig.ins().match[player.level].p+GlobalConfig.ins().match[player.level].o)){
        //     monsterQuality=QualityType.橙;
        // }
        // else if(ranNum<(GlobalConfig.ins().match[player.level].w+GlobalConfig.ins().match[player.level].g+GlobalConfig.ins().match[player.level].p+GlobalConfig.ins().match[player.level].o+GlobalConfig.ins().match[player.level].h)){
        //     monsterQuality=QualityType.黑;
        // }
        // else if(ranNum<(GlobalConfig.ins().match[player.level].w+GlobalConfig.ins().match[player.level].g+GlobalConfig.ins().match[player.level].p+GlobalConfig.ins().match[player.level].o+GlobalConfig.ins().match[player.level].h+GlobalConfig.ins().match[player.level].p)){
        //      isPlayer=true;
        // }
        // if(!isPlayer){
        //     return MonsterService.ins().getMonster(player.level,monsterQuality);
        // }
        let m:Monster=new Monster();
        m.init("21101",player.level);
        return m;
    }

    async openTreasure(msg){
        console.log("openTreasure",msg.id);
        let t:Treasure=null;
        let index=-1;
        let treasures:Treasure[]=await(msg.player.treasures);
        for(let i=0;i<treasures.length;i++){
            if(treasures[i]&&treasures[i].id===Number(msg.id)){
                t=treasures[i];
                index=i;
                break;
            }
        }
        if(t===null){
            return net_result.success(undefined,"宝箱已打开");
        }
        console.log("openTreasure....");
        let p:Player=msg.player;
        let rateJson=JSON.parse(GlobalConfig.ins().treasure[t.qualityType].rate);
        let qualityType=null;
        let randomNum=Math.random();
        if(randomNum<rateJson[0]){
            qualityType=QualityType.白;
        }
        else if(randomNum<(rateJson[0]+rateJson[1])){
            qualityType=QualityType.绿;
        }
        else if(randomNum<(rateJson[0]+rateJson[1]+rateJson[2])){
            qualityType=QualityType.紫;
        }
        else if(randomNum<(rateJson[0]+rateJson[1]+rateJson[2]+rateJson[3])){
            qualityType=QualityType.橙;
        }
        else if(randomNum<(rateJson[0]+rateJson[1]+rateJson[2]+rateJson[3]+rateJson[4])){
            qualityType=QualityType.黑;
        }
        let tR=await orm_util.getRepository(Treasure);

        delete (await p.treasures)[index];
        await tR.remove(t);
        if(qualityType!==null){
      
            let n=Math.floor(p.level/10);
            // console.log("n===",n);
            if(n===0){
                n=1;
            }
            else{
                n=Number(String(n)+"0");
            }
            let weapon:Weapon=<Weapon>p.equipms[0];
            let shield:Shield=<Shield>p.equipms[1];
                    //   console.log("w..s...",weapon,shield,"n="+n);
            let isGotWeapon=false;
            let isGotShield=false;
            if(weapon.level===n&&EquipmService.ins().weapons[n][qualityType].attack<=weapon.attack){
                isGotWeapon=true;
            }
            if(shield.level===n&&EquipmService.ins().shields[n][qualityType].hp<=shield.hp){
                isGotShield=true;
            }
                // console.log("openTreasure.."+qualityType+",attack=",EquipmService.ins().weapons[n][qualityType].attack+","+weapon.attack)
                // console.log("openTreasure..hp=",EquipmService.ins().shields[n][qualityType].hp+","+shield.hp)
                //   console.log("openTreasure..",isGotShield,isGotWeapon)

            if(isGotShield===false&&isGotWeapon===false){
                randomNum=Math.random();
                if(randomNum>0.5){
                    isGotShield=true;
                }
                else{
                    isGotWeapon=true;
                }
            }

            let pR=await orm_util.getRepository(Player);
            if(isGotShield&&isGotWeapon){
                let rewardMoney=Math.round(GlobalConfig.ins().treasure[qualityType].price*1.5);
                p.money+=rewardMoney;
                await pR.save(p);
                return net_result.success(undefined,{rewardMoney: rewardMoney});
            }
            else if (isGotWeapon&&!isGotShield){
                shield=p.changeEquipm(EquipmService.ins().shields[n][qualityType]);
                await pR.save(p);
                return net_result.success(undefined,{shield:shield,hp:p.maxHp});
            }
            else if(isGotShield&&!isGotWeapon){
                weapon=p.changeEquipm(EquipmService.ins().weapons[n][qualityType]);
                await pR.save(p);
                return net_result.success(undefined,{weapon:weapon,attack:p.attack});
            }
        }
        else{
            return net_result.success(undefined,"什么都没有");
        }
    }
   
}